//
//  AreaListVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/14.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "AreaListVC.h"
#import "AreaListModel.h"
#import "HospitalListVC.h"
#import "AreaListModel.h"

static NSString *tableViewCellIdentifier=@"UITableViewCellIdentifier";

@interface AreaListVC ()<UITableViewDataSource,UITableViewDelegate,CustomNavViewDelegate>

@property(nonatomic,strong)UITableView *areaListTableView;

@property(nonatomic,strong)NSMutableArray *areaListArr;

@end



@implementation AreaListVC

-(void)leftBtnEvent
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)rightBtnEvent
{
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _areaListArr=[NSMutableArray array];
    
    CustomNavView *jobFromYearNavView=[[CustomNavView alloc]initWithTitle:NSLocalizedString(@"Area for hospital", nil) leftBtnImage:@"back_gray" hasLeftBtnImage:YES rightBtnTitle:nil hasRightBtnTitle:NO];//@"选择所在地区"
    jobFromYearNavView.delegate=self;
    jobFromYearNavView.frame=CGRectMake(0,0,APPW,64);
    [self.view addSubview:jobFromYearNavView];
    
    [self initTableView];
    
    // 获取地区列表数据
   [self getAreaListData];
    
}

// 获取地区列表数据
-(void)getAreaListData
{
    [_areaListArr removeAllObjects];
    
    __weak typeof(self) weakSelf=self;
    
    NSDictionary *dic=@{
                        @"cid":_countryID
                        };
    
    [QBRequest GetAreaInfoWithDic:dic Success:^(id res)
    {
        //NSLog(@"201999----%@",res);
         weakSelf.areaListArr = [AreaListModel mj_objectArrayWithKeyValuesArray:res[@"info"]];

         [weakSelf.areaListTableView reloadData];

         [weakSelf.areaListTableView.mj_header endRefreshing];
        
    } failure:^(NSString *error)
    {
        
    }];
}

-(void)initTableView
{
    // _areaListTableView
    _areaListTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,64,APPW,APPH-64) style:UITableViewStylePlain];
    _areaListTableView.delegate=self;
    _areaListTableView.dataSource=self;
    _areaListTableView.hidden=NO;
    _areaListTableView.backgroundColor=WhiteColor;
    _areaListTableView.showsVerticalScrollIndicator=NO;
    _areaListTableView.tableFooterView=[UIView new];
    _areaListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_areaListTableView];
    [_areaListTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.areaListArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    AreaListModel *areaListModel=self.areaListArr[indexPath.row];
    
    BOOL isCN = [[LanguageConvert alloc] hasShownIsCnOrNot];
    if (isCN == YES) {//..
         cell.textLabel.text=areaListModel.cn_name;
    } else{
         cell.textLabel.text=areaListModel.en_name;
    }
//    cell.textLabel.text=areaListModel.cn_name;
    
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100/2;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    AreaListModel *areaListModel=self.areaListArr[indexPath.row];
    HospitalListVC *hospitalListVC=[HospitalListVC new];
    hospitalListVC.areaID=areaListModel.areaID;
    [self presentViewController:hospitalListVC animated:NO completion:nil];
    
}

@end
